package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppUserLevelBo;
import com.ruoyi.app.domain.vo.AppUserLevelVo;
import com.ruoyi.app.domain.AppUserLevel;
import com.ruoyi.app.mapper.AppUserLevelMapper;
import com.ruoyi.app.service.IAppUserLevelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * app用户等级Service业务层处理
 *
 * @author Zach
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
public class AppUserLevelServiceImpl implements IAppUserLevelService {

    private final AppUserLevelMapper baseMapper;

    /**
     * 查询app用户等级
     */
    @Override
    public AppUserLevelVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询app用户等级列表
     */
    @Override
    public TableDataInfo<AppUserLevelVo> queryPageList(AppUserLevelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppUserLevel> lqw = buildQueryWrapper(bo);
        Page<AppUserLevelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询app用户等级列表
     */
    @Override
    public List<AppUserLevelVo> queryList(AppUserLevelBo bo) {
        LambdaQueryWrapper<AppUserLevel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppUserLevel> buildQueryWrapper(AppUserLevelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppUserLevel> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), AppUserLevel::getName, bo.getName());
        lqw.eq(bo.getFrConditionNum() != null, AppUserLevel::getFrConditionNum, bo.getFrConditionNum());
        lqw.eq(bo.getFrConditionMoney() != null, AppUserLevel::getFrConditionMoney, bo.getFrConditionMoney());
        lqw.eq(bo.getRebate() != null, AppUserLevel::getRebate, bo.getRebate());
        lqw.eq(bo.getStatus() != null, AppUserLevel::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增app用户等级
     */
    @Override
    public Boolean insertByBo(AppUserLevelBo bo) {
        AppUserLevel add = BeanUtil.toBean(bo, AppUserLevel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改app用户等级
     */
    @Override
    public Boolean updateByBo(AppUserLevelBo bo) {
        AppUserLevel update = BeanUtil.toBean(bo, AppUserLevel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppUserLevel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除app用户等级
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
